// .........................................................................
// Title: coverage.do
//
// Plots the aggregate value of US investment-grade corporate bonds in the
// sample, and the corresponding coverage from the holdings micro-data
// .........................................................................

* ---------------------------------------
* Coverage aggregates
* ---------------------------------------

* Issuance in trace
use "$tmp/amounts_outstanding_y_mv", clear
keep if _in_trace == 1
keep if inrange(year, 2007, 2020)
mmerge cusip year using "$tmp/issue_ratings_merged_sp_moodys_y", unmatched(m)
keep if inlist(rating_cat, "AAA", "AA", "A", "BBB")
mmerge cusip using "$raw/cmns/gcap_security_master_cusip.dta", unmatched(m) ukeep(class_code2)
save "$tmp/trace_ig_amounts_outstanding", replace

* IG aggregates
use "$tmp/trace_ig_amounts_outstanding", clear
keep if class_code2 == "BC"
gcollapse (sum) value_outstanding, by(year)
replace value_outstanding = value_outstanding / 1e6
rename value_outstanding ig_aggregate
save "$tmp/ig_aggregates", replace

* insurance holdings, prep
use "$insurance_holdings/all_insurance_master", clear
rename entity_name holder_name
foreach var of varlist * {
    local try = lower("`var'")
    rename `var' `try'
}
mmerge cusip using "$tmp/trace_clean_catalog", unmatched(m)
keep holder_name cusip carrying_value conditional_fair_value actual_cost aggregate_par_value date_q marketvalue_usd in_trace_universe

* insurance holdings in trace, ig
drop if missing(cusip)
keep if in_trace_universe == 1
gcollapse (sum) aggregate_par_value marketvalue_usd conditional_fair_value actual_cost carrying_value, by(cusip date_q)
gen year = year(dofq(date_q))
gen quarter = quarter(dofq(date_q))
keep if quarter == 4
drop quarter
mmerge cusip using "$raw/cmns/gcap_security_master_cusip.dta", unmatched(m) ukeep(class_code1 class_code2)
keep if class_code2 == "BC"
mmerge cusip year using "$tmp/issue_ratings_merged_sp_moodys_y", unmatched(m)
keep if inlist(rating_cat, "AAA", "AA", "A", "BBB")
save "$tmp/insurer_trace_sec_summary_ig_y_upd", replace

* time series
use "$tmp/insurer_trace_sec_summary_ig_y_upd", clear
gcollapse (sum) aggregate_par_value marketvalue_usd, by(year)
foreach var of varlist aggregate_par_value marketvalue_usd {
    replace `var' = `var' / 1e9
}
replace marketvalue_usd = marketvalue_usd / 1e3

mmerge year using "$tmp/ig_aggregates", unmatched(m)
drop _merge
keep year aggregate_par_value marketvalue_usd ig_aggregate
order year aggregate_par_value marketvalue_usd ig_aggregate
rename aggregate_par_value insurers_par
rename marketvalue_usd insurers_market
gen share_par = insurers_par / ig_aggregate
gen share_market = insurers_market / ig_aggregate
save "$tmp/insurers_bench_timeseries_ig_upd", replace

* Collapse fund holdings to yearly
use "$tmp/morningstar_summary/mns_cusip_domicile_quarter_amounts", clear
gen year = year(dofq(date_q))
gen quarter = quarter(dofq(date_q))
keep if quarter == 4
drop quarter date_q
save "$tmp/morningstar_summary/mns_cusip_domicile_quarter_amounts_y", replace

* fund holdings in trace
use "$tmp/morningstar_summary/mns_cusip_domicile_quarter_amounts_y", clear
mmerge cusip using "$raw/cmns/gcap_security_master_cusip.dta", unmatched(m) ukeep(class_code1 class_code2)
mmerge cusip using "$tmp/trace_clean_catalog", unmatched(m)
mmerge cusip year using "$tmp/issue_ratings_merged_sp_moodys_y", unmatched(m)
keep if class_code2 == "BC"
keep if inlist(rating_cat, "AAA", "AA", "A", "BBB")
keep if in_trace_universe == 1
gen domestic = 0
replace domestic = 1 if Domicile == "USA"
gcollapse (sum) marketvalue_usd, by(year domestic cusip)
save "$tmp/funds_trace_sec_summary_q_ig", replace

* time series
use "$tmp/funds_trace_sec_summary_q_ig", clear
mmerge cusip year using "$tmp/trace_clean_prices_y", unmatched(m)
gen parvalue_usd = marketvalue_usd
replace parvalue_usd = marketvalue_usd / (trace_median_price_w / 100) if ~missing(trace_median_price_w) & trace_median_price_w > 0
gcollapse (sum) marketvalue_usd parvalue_usd, by(year domestic)
foreach var of varlist marketvalue_usd parvalue_usd {
    replace `var' = `var' / 1e12
}
reshape wide marketvalue_usd parvalue_usd, i(year) j(domestic)

rename marketvalue_usd0 mv_foreign
rename marketvalue_usd1 mv_domestic
rename parvalue_usd0 pv_foreign
rename parvalue_usd1 pv_domestic
gen mv_tot = mv_domestic + mv_foreign
gen pv_tot = pv_domestic + pv_foreign
mmerge year using "$tmp/ig_aggregates", unmatched(m)
drop _merge
gen share_micro = pv_tot / ig_aggregate
keep year pv* share_micro ig_aggregate

rename pv_tot funds_tot_par
rename pv_domestic funds_domestic_par
rename pv_foreign funds_foreign_par
rename share_micro funds_share_tot

gen funds_share_domestic = funds_domestic_par / ig_aggregate
gen funds_share_foreign = funds_foreign_par / ig_aggregate
drop ig_aggregate
save "$tmp/funds_bench_series_ig", replace


* merge funds and insurers
use "$tmp/funds_bench_series_ig", clear
mmerge year using "$tmp/insurers_bench_timeseries_ig_upd"
keep if _merge == 3
drop _merge
cap drop *share*
drop if year < 2007

gen insurers_plus_domestic_funds = insurers_par + funds_domestic_par
gen insurers_plus_tot_funds = insurers_par + funds_domestic_par + funds_foreign_par

gen share_insurers = insurers_par / ig_aggregate
gen share_funds_domestic = funds_domestic_par / ig_aggregate
gen share_funds_foreign = funds_foreign_par / ig_aggregate
gen tot_coverage = (insurers_par + funds_domestic_par + funds_foreign_par) / ig_aggregate
gen share_other = 1 - (share_insurers + share_funds_domestic + share_funds_foreign)

label var ig_aggregate "Other Holders"
label var insurers_plus_domestic_funds "Domestic Mutual Funds and ETFs"
label var insurers_plus_tot_funds "Foreign Mutual Funds and ETFs"
label var insurers_par "Domestic Insurers"

label var share_insurers "Domestic Insurers"
label var share_funds_domestic "Domestic Mutual Funds and ETFs"
label var share_funds_foreign "Foreign Mutual Funds and ETFs"
label var share_other "Other Holders"

rename funds_domestic_par funds_domestic
rename funds_foreign_par funds_foreign
rename insurers_par insurers

keep ig_aggregate insurers_plus_tot_funds insurers_plus_domestic_funds insurers share_other ///
    funds_domestic funds_foreign share_insurers share_funds_domestic share_funds_foreign year

save "$tmp/coverage_stats", replace

* plot in levels
use "$tmp/coverage_stats", clear
twoway (area ig_aggregate insurers_plus_tot_funds insurers_plus_domestic_funds insurers year, ///
    graphregion(color(white) margin(1 10 2 1)) xlab(2007(1)2020, angle(45)) ///
    title("(A) Levels" " ", size(medsmall) color(black)) ///
    color("189 201 225" "116 169 207" "43 140 190" "4 90 141") xtitle("") legend(cols(2) order(4 3 1 2)) ///
    ylab(0(1)8, angle(0)) ytitle("USD Trillion" " ") xsize(6) saving("$tmp/coverage_levels_panel.gph", replace)) 

* plot in shares
use "$tmp/coverage_stats", clear
line share_insurers share_funds_domestic share_funds_foreign share_other year, graphregion(color(white) margin(1 10 2 1)) ///
    xlab(2007(2)2020) xtitle("") ylab(0 "0%" .1 "10%" .2 "20%" .3 "30%" .4 "40%" .5 "50%" .6 "60%", gmax) ///
    legend(cols(3)) lcolor(blue red green black%60) ///
    lpattern(solid dash solid shortdash) ytitle("Sector Share") xsize(6) xlab(2007(1)2020, angle(45)) ///
    title("(B) Shares" " ", size(medsmall) color(black)) legend(cols(2)) saving("$tmp/coverage_shares_panel.gph", replace) ///
    legend(order(1 2 4 3))

* combine the panels and save (Figure 1)
gr combine "$tmp/coverage_levels_panel.gph" "$tmp/coverage_shares_panel.gph", xsize(12) iscale(1)
graph export "$graphs/coverage.pdf", as(pdf) replace
